package com.jlboot.app.admin.controller.sys;

import com.jlboot.core.annotation.JlActivity;
import com.jlboot.core.util.CrudUtil;
import com.jlboot.model.po.sys.SysUser;
import com.jlboot.core.json.JsonResult;
import com.jlboot.core.util.DateUtil;
import com.jlboot.app.admin.base.BaseController;
import com.jlboot.core.base.PageSearch;
import com.jlboot.core.form.FormData;
import com.jlboot.core.form.FormSelect;

import com.jlboot.core.cache.SysUserCache;
import com.jlboot.model.po.sys.SysWorkLog;
import com.jlboot.app.admin.ui.plugins.layui.LayuiData;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

import java.util.Date;
import java.util.List;

/**
 * Created by 51594 on 2020/4/22.
 */
public class SysWorkLogController extends BaseController {


    public void index() {
        String id = String.valueOf(getAttr("id", ""));
        SysUser user = SysUser.dao.findById(id);
        setAttr("form", FormData.n().addData("user", user));
        //setAttr("R", ValidateRole.n(LoginUtil.getJwt(getRequest())));
        render("Index.html");
    }


    /**
     * 列表
     */
    
    public void SysWorkLogList() {
        FormData fd = FormData.n();
        setAttr("form", fd);
        render("SysLoginLogList.html");
    }

    /**
     * 操作日志列表
     */
    @JlActivity( name = "操作日志列表", log = false)
    public void SysOperLogList() {
        FormData fd = FormData.n();
        setAttr("form", fd);
        render("SysOperLogList.html");
    }


    /**
     * 登陆日志列表
     */
    @JlActivity( name = "登陆日志列表", log = false)
    public void SysLoginLogList() {
        FormData fd = FormData.n();
        setAttr("form", fd);
        render("SysLoginLogList.html");
    }

    /**
     * 登陆日志查询API
     */
    @JlActivity( name = "登陆日志查询API", log = false)
    public void SysOperLogListDataApi() {
        PageSearch ps = PageSearch.n().init(getRequest(), "sys_work_log");
        ps.search("log_type", "<", 98);
        ps.orderby(getRequest(), "id desc");
        Page<SysWorkLog> pages = ps.paginate(SysWorkLog.dao);
        List<SysWorkLog> datalist = pages.getList();
        FormSelect fs = FormSelect.n().setSource("`1`成功`2`失败`").setColor("`1`layui-bg-green`2`layui-bg-orange`").setReadOnly(true);
        for (SysWorkLog wl : datalist) {
            wl.put("emp_str", SysUserCache.transUserName(wl.mod.getLong("id")));
            wl.put("log_result_str", fs.showText(wl.mod.getInt("log_result")));
            wl.put("log_result_color", wl.mod.getInt("log_result") == 1 ? "layui-bg-green" : "");
        }
        renderJson(LayuiData.newData().setData(datalist).setCount(pages.getTotalRow()));
    }

    /**
     * 登陆日志查询API
     */
    @JlActivity( name = "登陆日志查询API", log = false)
    public void SysLoginLogListDataApi() {
        PageSearch ps = PageSearch.n().init(getRequest(), "sys_work_log");
        ps.search("log_type", 99);
        ps.orderby(getRequest(), "id desc");
        Page<SysWorkLog> pages = ps.paginate(SysWorkLog.dao);
        List<SysWorkLog> datalist = pages.getList();
        FormSelect fs = FormSelect.n().setSource("`1`成功`2`失败`").setColor("`1`layui-bg-green`2`layui-bg-orange`").setReadOnly(true);
        for (SysWorkLog wl : datalist) {
            wl.put("emp_str", SysUserCache.transUserName(wl.mod.getLong("id")));
            wl.put("log_result_str", fs.showText(wl.mod.getInt("log_result")));
            wl.put("log_result_color", wl.mod.getInt("log_result") == 1 ? "layui-bg-green" : "");
        }
        renderJson(LayuiData.newData().setData(datalist).setCount(pages.getTotalRow()));
    }


    /**
     * 清空登陆日志Api
     */
    @JlActivity( name = "清空登陆日志Api", log = false)
    public void EmptyLoginLogApi() {
        int days = getParaInt("days", 365);
        if (days < 1) days = 1;
        Date log_time = DateUtil.addDay(new Date(), (-1 * days));
        Db.delete("delete from sys_work_log where log_type=99 and log_time<?", log_time);
        renderJson(JsonResult.okWithMsg("清除日志成功"));
    }

    /**
     * 清空操作日志Api
     */
    @JlActivity( name = "清空操作日志Api", log = false)
    public void EmptyOperLogApi() {
        int days = getParaInt("days", 365);
        if (days < 1) days = 1;
        Date log_time = DateUtil.addDay(new Date(), (-1 * days));
        Db.delete("delete from sys_work_log where log_type<99 and log_time<?", log_time);
        renderJson(JsonResult.okWithMsg("清除日志成功"));
    }

    /**
     * 批量删除操作日志Api
     */
    @JlActivity( name = "批量删除操作日志Api", log = false)
    public void DelMoreOperLogApi() {
        Integer[] result= CrudUtil.DelMoreModel(SysWorkLog.table,getParaStr("keys", "").split(","));
        renderJson(JsonResult.okWithMsg("删除成功，【共删除成功 " + result[0] + ",失败 " + result[1] + "】"));
    }
}